﻿using BCCommon;
using BCCommon.Enums;
using BCData.TMS.VehicleBusinessMapping;
using BCDto.TMS.VehicleBusinessMapping;
using BCEntity.TMS.VehicleBusinessMapping;
using BCEntity.Common.EntityNameConst;
using BCService.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BCDto.Sim.Admin.Admin;
using BCService.TMS.VehicleTypeService;
using BCData.TMS.VehicleTransportBusinessConfig;
using BCService.Common.Area;
using BCService.TMS.Agent.AgentAreaRecord;
using BCCommon.TMS.Transport;
using BCData.TMS.VehicleCategoryReference;
using BCEntity.TMS.VehicleCategoryReference;

namespace BCService.TMS.VehicleBusinessMapping
{
    public class VehicleBusinessMappingService : IVehicleBusinessMappingService
    {
        private readonly IDatabaseContext databaseContext;
        private readonly IRedisService redisService;
        private readonly IVehicleBusinessMappingData vehicleBusinessMappingData;
        private readonly IVehicleBusinessMappingOverwriteData vehicleBusinessMappingOverwriteData;
        private readonly IVehicleTransportBusinessConfigData vehicleTransportBusinessConfigData;
        private readonly IVehicleTypeService vehicleTypeService;
        private readonly IZoneService zoneService;
        private readonly IAgentAreaRecordService agentAreaRecordService;
        private readonly IVehicleCategoryReferenceData vehicleCategoryReferenceData;
        private readonly string key = string.Format("{0}", EntityNameConst.VehicleBusinessMappingEntity);

        public VehicleBusinessMappingService(IDatabaseContext databaseContext,
            IRedisService redisService,
            IVehicleBusinessMappingData vehicleBusinessMappingData,
            IVehicleBusinessMappingOverwriteData vehicleBusinessMappingOverwriteData,
            IVehicleTransportBusinessConfigData vehicleTransportBusinessConfigData,
            IVehicleTypeService vehicleTypeService,
            IZoneService zoneService,
            IAgentAreaRecordService agentAreaRecordService,
            IVehicleCategoryReferenceData vehicleCategoryReferenceData)
        {
            this.databaseContext = databaseContext;
            this.redisService = redisService;
            this.vehicleBusinessMappingData = vehicleBusinessMappingData;
            this.vehicleBusinessMappingOverwriteData = vehicleBusinessMappingOverwriteData;
            this.vehicleTransportBusinessConfigData = vehicleTransportBusinessConfigData;
            this.vehicleTypeService = vehicleTypeService;
            this.zoneService = zoneService;
            this.agentAreaRecordService = agentAreaRecordService;
            this.vehicleCategoryReferenceData = vehicleCategoryReferenceData;
        }

        #region 检查车辆与业务配置是否存在
        public bool Exists(int vehicleBusinessMappingId)
        {
            return this.vehicleBusinessMappingData.Exists(vehicleBusinessMappingId).GetAwaiter().GetResult();
        }
        private bool InUsed(int vehicleBusinessMappingId)
        {
            return this.vehicleBusinessMappingOverwriteData.InUsed(vehicleBusinessMappingId).GetAwaiter().GetResult();
        }
        #endregion

        #region 添加车辆与业务配置
        public VehicleBusinessMappingDto Add(AdminDto adminDto, VehicleBusinessMappingRequestDto requestDto)
        {
            if (adminDto.AgentRecord != null)
            {
                throw new ArgumentException("代理商无权添加车辆与业务配置信息!");
            }

            var vehicleTypeDto = this.vehicleTypeService.GetById(requestDto.VehicleTypeId);
            if (vehicleTypeDto == null)
            {
                throw new ArgumentException("车辆类型信息不存在!");
            }
            var transportBusinessConfigEntity = this.vehicleTransportBusinessConfigData.GetEntity(requestDto.VehicleTransportBusinessConfigId).Result;
            if (transportBusinessConfigEntity == null)
            {
                throw new ArgumentException("此运输业务配置信息不存在!");
            }
            if (requestDto.MaxWithCarPeople > 2)
            {
                throw new ArgumentException("最大跟车人数不允许大于2人!");
            }
            string[] containsVehicleTypeString = new string[] { "电动车", "电瓶车", "摩托车" };

            VehicleCategoryReferenceExpandEntity vehicleCategoryReference = null;
            if (!containsVehicleTypeString.Contains(vehicleTypeDto.VehicleTypeValue))
            {
                if (!requestDto.VehicleCategoryReferenceId.HasValue)
                {
                    throw new ArgumentException("非电动车，车辆分类不可为空!");
                }
                vehicleCategoryReference = this.vehicleCategoryReferenceData.GetById(requestDto.VehicleCategoryReferenceId.Value).Result;
                if (vehicleCategoryReference == null)
                {
                    throw new ArgumentException("车辆所属分类信息不存在");
                }
            }
            var entity = requestDto.As<VehicleBusinessMappingEntity>();
            entity.CreateAdminName = adminDto.FullName;
            entity.CreateAdminId = adminDto.AdminId;
            entity.VehicleTransportBusinessConfigId = transportBusinessConfigEntity.VehicleTransportBusinessConfigId;
            entity.TransportBusiness = (TransportBusinessType)Enum.Parse(typeof(TransportBusinessType), transportBusinessConfigEntity.TransportBusiness);
            entity.VehicleTypeId = vehicleTypeDto.VehicleTypeId;
            entity.VehicleTypeName = vehicleTypeDto.VehicleTypeName;
            entity.Enabled = true;
            entity.VehicleCategoryReferenceId = vehicleCategoryReference.VehicleCategoryReferenceId;
            entity.VehicleCategory = vehicleCategoryReference.VehicleCategory;
            entity.VehicleCategoryName = vehicleCategoryReference.CategoryValue;
            return this.vehicleBusinessMappingData.Add(entity).GetAwaiter().GetResult().As<VehicleBusinessMappingDto>();
        }
        #endregion

        #region 修改车辆与业务配置
        public VehicleBusinessMappingDto Update(AdminDto adminDto, VehicleBusinessMappingPutDto putDto)
        {
            if (!this.Exists(putDto.VehicleBusinessMappingId))
            {
                throw new ArgumentException("此车辆与业务配置信息不存在!");
            }
            if (this.InUsed(putDto.VehicleBusinessMappingId))
            {
                throw new ArgumentException("此车辆与业务配置信息已存在使用中记录，不允许再修改此记录!");
            }
            var vehicleTypeDto = this.vehicleTypeService.GetById(putDto.VehicleTypeId);
            if (vehicleTypeDto == null)
            {
                throw new ArgumentException("车辆类型信息不存在!");
            }
            var vehicleBusinessMappingEntity = this.vehicleBusinessMappingData.GetEntity(putDto.VehicleBusinessMappingId).Result;
            if (vehicleBusinessMappingEntity == null)
            {
                throw new ArgumentException("此运输业务配置信息不存在!");
            }
            var transportBusinessConfigEntity = this.vehicleTransportBusinessConfigData.GetEntity(vehicleBusinessMappingEntity.VehicleTransportBusinessConfigId).Result;
            if (transportBusinessConfigEntity == null)
            {
                throw new ArgumentException("此运输业务配置信息不存在!");
            }
            string[] containsVehicleTypeString = new string[] { "电动车", "电瓶车", "摩托车" };

            VehicleCategoryReferenceExpandEntity vehicleCategoryReference = null;
            if (!containsVehicleTypeString.Contains(vehicleTypeDto.VehicleTypeValue))
            {
                if (!putDto.VehicleCategoryReferenceId.HasValue)
                {
                    throw new ArgumentException("非电动车，车辆分类不可为空!");
                }
                vehicleCategoryReference = this.vehicleCategoryReferenceData.GetById(putDto.VehicleCategoryReferenceId.Value).Result;
                if (vehicleCategoryReference == null)
                {
                    throw new ArgumentException("车辆所属分类信息不存在");
                }
            }
            var entity = putDto.As<VehicleBusinessMappingEntity>();
            entity.EditAdminName = adminDto.FullName;
            entity.EditAdminId = adminDto.AdminId;
            entity.VehicleTransportBusinessConfigId = vehicleBusinessMappingEntity.VehicleTransportBusinessConfigId;
            entity.TransportBusiness = (TransportBusinessType)Enum.Parse(typeof(TransportBusinessType), transportBusinessConfigEntity.TransportBusiness);
            entity.VehicleTypeId = vehicleTypeDto.VehicleTypeId;
            entity.VehicleTypeName = vehicleTypeDto.VehicleTypeName;
            entity.Enabled = true;
            entity.VehicleCategoryReferenceId = vehicleCategoryReference==null?new Nullable<int>(): vehicleCategoryReference.VehicleCategoryReferenceId;
            entity.VehicleCategory = vehicleCategoryReference==null?string.Empty: vehicleCategoryReference.VehicleCategory;
            entity.VehicleCategoryName = vehicleCategoryReference == null ? string.Empty: vehicleCategoryReference.CategoryValue;
            var result = this.vehicleBusinessMappingData.Update(entity).GetAwaiter().GetResult().As<VehicleBusinessMappingDto>();
            string cacheKey = this.redisService.GetKey(key, entity.VehicleBusinessMappingId);
            this.redisService.DeleteAsync(cacheKey);
            return result;
        }
        #endregion

        //#region 修改车辆与业务配置(重写)
        //public VehicleBusinessMappingDto Update(AdminDto adminDto, VehicleBusinessMappingOverwriteDto putDto)
        //{
        //    var zoneEntity = this.zoneService.GetFromZoneEntity(putDto.CountyCode);
        //    if (zoneEntity == null || zoneEntity.countyEntity == null || zoneEntity.provinceEntity == null || zoneEntity.cityEntity == null)
        //    {
        //        throw new ArgumentException("此区县编码不正确");
        //    }
        //    if (!this.agentAreaRecordService.CheckCountyAuthorize(zoneEntity.countyEntity.CountyCode, adminDto.AgentRecord.AgentRecordId))
        //    {
        //        throw new ArgumentException("此区域授权失败!");
        //    }
        //    if (!this.Exists(putDto.VehicleBusinessMappingId))
        //    {
        //        throw new ArgumentException("此车辆与业务配置信息不存在!");
        //    }

        //    var vehicleBusinessMappingEntity = this.vehicleBusinessMappingData.GetEntity(putDto.VehicleBusinessMappingId).Result;
        //    if (vehicleBusinessMappingEntity == null)
        //    {
        //        throw new ArgumentException("此运输业务配置信息不存在!");
        //    }
        //    var transportBusinessConfigEntity = this.vehicleTransportBusinessConfigData.GetEntity(vehicleBusinessMappingEntity.VehicleTransportBusinessConfigId).Result;
        //    if (transportBusinessConfigEntity == null)
        //    {
        //        throw new ArgumentException("此运输业务配置信息不存在!");
        //    }
        //    var entity = putDto.As<VehicleBusinessMappingEntity>();
        //    entity.EditAdminName = adminDto.FullName;
        //    entity.EditAdminId = adminDto.AdminId;
        //    entity.VehicleTransportBusinessConfigId = vehicleBusinessMappingEntity.VehicleTransportBusinessConfigId;
        //    entity.TransportBusiness = (TransportBusinessType)Enum.Parse(typeof(TransportBusinessType), transportBusinessConfigEntity.TransportBusiness);
        //    entity.VehicleTypeId = vehicleBusinessMappingEntity.VehicleTypeId;
        //    entity.VehicleTypeName = vehicleBusinessMappingEntity.VehicleTypeName;
        //    entity.Enabled = true;
        //    entity.ProvinceCode = zoneEntity.provinceEntity.ProvinceCode;
        //    entity.CityCode = zoneEntity.cityEntity.CityCode;

        //    var result = this.vehicleBusinessMappingOverwriteData.Add(entity).GetAwaiter().GetResult().As<VehicleBusinessMappingDto>();
        //    string cacheKey = this.redisService.GetKey(key, entity.VehicleBusinessMappingId, result.VehicleBusinessMappingOverwriteId);
        //    this.redisService.DeleteAsync(cacheKey);
        //    return result;
        //}
        //#endregion

        #region 删除车辆与业务配置
        public bool Delete(AdminDto adminDto, int vehicleBusinessMappingId)
        {
            if (adminDto.AgentRecord != null)
            {
                throw new ArgumentException("代理商无权执行此操作!");
            }
            if (!this.Exists(vehicleBusinessMappingId))
            {
                throw new ArgumentException("此记录不存在!");
            }
            var result = this.vehicleBusinessMappingData.Delete(vehicleBusinessMappingId).GetAwaiter().GetResult();
            string cacheKey = this.redisService.GetKey(key, vehicleBusinessMappingId);
            this.redisService.DeleteAsync(cacheKey);
            return result;
        }
        #endregion

        #region 获取车辆与业务配置
        public VehicleBusinessMappingDto Get(int vehicleBusinessMappingId)
        {
            string cacheKey = this.redisService.GetKey(key, vehicleBusinessMappingId);
            var result = this.redisService.TryGet(cacheKey, () =>
            {
                var data = this.vehicleBusinessMappingData.GetEntity(vehicleBusinessMappingId).GetAwaiter().GetResult();
                return data;
            }, TimeSpan.FromDays(1));
            return result.As<VehicleBusinessMappingDto>();
        }
        #endregion

        #region 获取车辆与业务配置列表
        public IEnumerable<VehicleBusinessMappingDto> GetList()
        {
            string cacheKey = this.redisService.GetKey(key);
            var result = this.redisService.TryGet(cacheKey, () =>
            {
                return this.vehicleBusinessMappingData.GetList().GetAwaiter().GetResult();
            }, TimeSpan.FromDays(1));
            return result.As<IEnumerable<VehicleBusinessMappingDto>>();
        }
        #endregion

        #region 获取车辆与业务配置分页
        public PageableList<VehicleBusinessMappingDto> Query(int? vehicleTransportBusinessConfigId, int? vehicleTypeId, bool? enabled, DateTime? start, DateTime? end, string sortName, bool? ascending, int pageIndex, int pageSize)
        {
                string cacheKey = this.redisService.GetKey(key, vehicleTransportBusinessConfigId, vehicleTypeId, enabled, start, end, sortName, ascending, pageIndex, pageSize);
                var result = this.redisService.TryGet(cacheKey, () =>
                {
                    var data = this.vehicleBusinessMappingData.Query(vehicleTransportBusinessConfigId, vehicleTypeId, enabled, start, end, sortName, ascending, pageIndex, pageSize).GetAwaiter().GetResult();
                    return new PageableList<VehicleBusinessMappingDto>
                    {
                        Count = data.Item2,
                        Items = data.Item1.As<IEnumerable<VehicleBusinessMappingDto>>(),
                        PageIndex = pageIndex,
                        PageSize = pageSize,
                        ServerTime = DateTime.Now
                    };
                }, TimeSpan.FromMinutes(30));
                return result;
            
                //string cacheKey = this.redisService.GetKey(key, adminDto.AgentRecord.AgentRecordId, countyCode, vehicleTransportBusinessConfigId, vehicleTypeId, enabled, start, end, sortName, ascending, pageIndex, pageSize);
                //var result = this.redisService.TryGet(cacheKey, () =>
                //{
                //    string agentCacheKey = string.Format("{0}:All:AgentRecordId={1}", key, adminDto.AgentRecord.AgentRecordId);
                //    var agentResult = this.redisService.TryGet(agentCacheKey, () =>
                //     {
                //         var overwriteList = this.vehicleBusinessMappingOverwriteData.GetList(adminDto.AgentRecord.AgentRecordId).GetAwaiter().GetResult();
                //         var sourceList = this.vehicleBusinessMappingData.GetList().GetAwaiter().GetResult();
                //         List<VehicleBusinessMappingEntity> viewList = new List<VehicleBusinessMappingEntity>();
                //         foreach (var item in sourceList)
                //         {
                //             if (overwriteList.Any(p => p.VehicleBusinessMappingId == item.VehicleBusinessMappingId && p.Enabled))
                //             {
                //                 viewList.Add(item);
                //             }
                //             else if (item.Enabled)
                //             {
                //                 viewList.Add(item);
                //             }
                //         }
                //         return viewList.As<IEnumerable<VehicleBusinessMappingEntity>>();

                //     }, TimeSpan.FromDays(1));

                //    if (!string.IsNullOrWhiteSpace(countyCode))
                //    {
                //        agentResult = agentResult.Where(p => p.CountyCode == countyCode);
                //    }
                //    if (vehicleTransportBusinessConfigId.HasValue)
                //    {
                //        agentResult = agentResult.Where(p => p.VehicleTransportBusinessConfigId == vehicleTransportBusinessConfigId);
                //    }
                //    if (vehicleTypeId.HasValue)
                //    {
                //        agentResult = agentResult.Where(p => p.VehicleTypeId == vehicleTypeId);
                //    }
                //    if (enabled.HasValue)
                //    {
                //        agentResult = agentResult.Where(p => p.Enabled == enabled);
                //    }
                //    if (start.HasValue && end.HasValue)
                //    {
                //        agentResult = agentResult.Where(p => p.CreateTime >= start && p.CreateTime <= end);
                //    }
                //    else if (start.HasValue && !end.HasValue)
                //    {
                //        agentResult = agentResult.Where(p => p.CreateTime >= start);
                //    }
                //    else if (end.HasValue)
                //    {
                //        agentResult = agentResult.Where(p => p.CreateTime <= end);
                //    }
                //    int count = agentResult.Count();
                //    if (!string.IsNullOrEmpty(sortName))
                //    {
                //        bool descending = ascending.HasValue ? !ascending.Value : false;
                //        var list = agentResult.AsQueryable().OrderBy(sortName, descending).Skip((pageIndex > 0 ? pageIndex - 1 : 0) * pageSize).Take(pageSize);
                //        return new PageableList<VehicleBusinessMappingDto>
                //        {
                //            Count = count,
                //            Items = list.As<IEnumerable<VehicleBusinessMappingDto>>(),
                //            PageIndex = pageIndex,
                //            PageSize = pageSize,
                //            ServerTime = DateTime.Now
                //        };
                //    }
                //    else
                //    {
                //        var list = agentResult.OrderByDescending(p => p.CreateTime).Skip((pageIndex > 0 ? pageIndex - 1 : 0) * pageSize).Take(pageSize);
                //        return new PageableList<VehicleBusinessMappingDto>
                //        {
                //            Count = count,
                //            Items = list.As<IEnumerable<VehicleBusinessMappingDto>>(),
                //            PageIndex = pageIndex,
                //            PageSize = pageSize,
                //            ServerTime = DateTime.Now
                //        };
                //    }
                //    //var data = this.vehicleBusinessMappingOverwriteData.Query(adminDto.AgentRecord.AgentRecordId, countyCode, vehicleTransportBusinessConfigId, vehicleTypeId, enabled, start, end, sortName, ascending, pageIndex, pageSize).GetAwaiter().GetResult();
                //    //return new PageableList<VehicleBusinessMappingDto>
                //    //{
                //    //    Count = data.Item2,
                //    //    Items = data.Item1.As<IEnumerable<VehicleBusinessMappingDto>>(),
                //    //    PageIndex = pageIndex,
                //    //    PageSize = pageSize,
                //    //    ServerTime = DateTime.Now
                //    //};

                //}, TimeSpan.FromMinutes(30));
                //return result;
            
        }
        #endregion

        #region 启用车辆与业务配置
        public bool Enabled(AdminDto adminDto, int vehicleBusinessMappingId, int? vehicleBusinessMappingOverwriteId, string countyCode)
        {
            if (adminDto.AgentRecord == null)
            {
                if (!this.Exists(vehicleBusinessMappingId))
                {
                    throw new ArgumentException("此记录不存在!");
                }
                var result = this.vehicleBusinessMappingData.Enabled(vehicleBusinessMappingId, adminDto.AdminId, adminDto.FullName).GetAwaiter().GetResult();
                string cacheKey = this.redisService.GetKey(key, vehicleBusinessMappingId);
                this.redisService.DeleteAsync(cacheKey);
                return result;
            }
            else
            {
                if (vehicleBusinessMappingOverwriteId.HasValue)
                {
                    var oldEntity = this.vehicleBusinessMappingOverwriteData.GetEntity(vehicleBusinessMappingOverwriteId.Value).Result;
                    if (oldEntity == null)
                    {
                        throw new ArgumentException("此记录不存在!");
                    }
                    if (adminDto.AgentRecord.AgentRecordId != oldEntity.AgentRecordId)
                    {
                        throw new ArgumentException("此区域您无权修改!");
                    }
                    var quickResult = this.vehicleBusinessMappingOverwriteData.Enabled(oldEntity.VehicleBusinessMappingOverwriteId.Value, adminDto.AdminId, adminDto.FullName).GetAwaiter().GetResult();
                    if (!quickResult)
                    {
                        throw new ArgumentException("启用失败!");
                    }
                    string quickCacheKey = this.redisService.GetKey(key, vehicleBusinessMappingId, vehicleBusinessMappingOverwriteId);
                    this.redisService.DeleteAsync(quickCacheKey);
                    return quickResult;
                }
                if (string.IsNullOrWhiteSpace(countyCode))
                {
                    throw new ArgumentException("请选择要设置业务的行政区域!");
                }
                var zoneEntity = this.zoneService.GetFromZoneEntity(countyCode);
                if (zoneEntity == null || zoneEntity.provinceEntity == null || zoneEntity.cityEntity == null || zoneEntity.countyEntity == null)
                {
                    throw new ArgumentException("省市区信息不正确!");
                }
                if (this.agentAreaRecordService.CheckCountyAuthorize(zoneEntity.countyEntity.CountyCode, adminDto.AgentRecord.AgentRecordId))
                {
                    throw new ArgumentException("此区域您无权修改!");
                }
                var entity = this.vehicleBusinessMappingOverwriteData.GetEntity(vehicleBusinessMappingId, adminDto.AgentRecord.AgentRecordId).GetAwaiter().GetResult();
                this.databaseContext.BeginTransaction();
                if (entity == null)
                {
                    var mappingEntity = this.vehicleBusinessMappingData.GetEntity(vehicleBusinessMappingId).GetAwaiter().GetResult();
                    if (mappingEntity == null)
                    {
                        throw new ArgumentException("此记录不存在!");
                    }
                    mappingEntity.AgentRecordId = adminDto.AgentRecord.AgentRecordId;
                    mappingEntity.ProvinceCode = zoneEntity.provinceEntity.ProvinceCode;
                    mappingEntity.CityCode = zoneEntity.cityEntity.CityCode;
                    mappingEntity.CountyCode = zoneEntity.countyEntity.CountyCode;
                    entity = this.vehicleBusinessMappingOverwriteData.Add(mappingEntity).GetAwaiter().GetResult();
                    if (entity == null)
                    {
                        throw new ArgumentException("启用失败!");
                    }
                }
                var result = this.vehicleBusinessMappingOverwriteData.Enabled(entity.VehicleBusinessMappingOverwriteId.Value, adminDto.AdminId, adminDto.FullName).GetAwaiter().GetResult();
                if (!result)
                {
                    throw new ArgumentException("启用失败!");
                }
                this.databaseContext.Commit();

                string cacheKey = this.redisService.GetKey(key, vehicleBusinessMappingId, entity.VehicleBusinessMappingOverwriteId);
                this.redisService.DeleteAsync(cacheKey);
                return result;
            }

        }
        #endregion

        #region 禁用车辆与业务配置
        public bool Disabled(AdminDto adminDto, int vehicleBusinessMappingId, int? vehicleBusinessMappingOverwriteId, string countyCode)
        {
            if (adminDto.AgentRecord == null)
            {
                if (!this.Exists(vehicleBusinessMappingId))
                {
                    throw new ArgumentException("此记录不存在!");
                }
                var result = this.vehicleBusinessMappingData.Disabled(vehicleBusinessMappingId, adminDto.AdminId, adminDto.FullName).GetAwaiter().GetResult();
                string cacheKey = this.redisService.GetKey(key, vehicleBusinessMappingId);
                this.redisService.DeleteAsync(cacheKey);
                return result;
            }
            else
            {
                if (vehicleBusinessMappingOverwriteId.HasValue)
                {
                    var oldEntity = this.vehicleBusinessMappingOverwriteData.GetEntity(vehicleBusinessMappingOverwriteId.Value).Result;
                    if (oldEntity == null)
                    {
                        throw new ArgumentException("此记录不存在!");
                    }
                    if (adminDto.AgentRecord.AgentRecordId != oldEntity.AgentRecordId)
                    {
                        throw new ArgumentException("此区域您无权修改!");
                    }
                    var quickResult = this.vehicleBusinessMappingOverwriteData.Disabled(oldEntity.VehicleBusinessMappingOverwriteId.Value, adminDto.AdminId, adminDto.FullName).GetAwaiter().GetResult();
                    if (!quickResult)
                    {
                        throw new ArgumentException("禁用失败!");
                    }
                    string quickCacheKey = this.redisService.GetKey(key, vehicleBusinessMappingId, vehicleBusinessMappingOverwriteId);
                    this.redisService.DeleteAsync(quickCacheKey);
                    return quickResult;
                }
                if (string.IsNullOrWhiteSpace(countyCode))
                {
                    throw new ArgumentException("请选择要设置业务的行政区域!");
                }
                var zoneEntity = this.zoneService.GetFromZoneEntity(countyCode);
                if (zoneEntity == null || zoneEntity.provinceEntity == null || zoneEntity.cityEntity == null || zoneEntity.countyEntity == null)
                {
                    throw new ArgumentException("省市区信息不正确!");
                }
                if (this.agentAreaRecordService.CheckCountyAuthorize(zoneEntity.countyEntity.CountyCode, adminDto.AgentRecord.AgentRecordId))
                {
                    throw new ArgumentException("此区域您无权修改!");
                }
                var entity = this.vehicleBusinessMappingOverwriteData.GetEntity(vehicleBusinessMappingId, adminDto.AgentRecord.AgentRecordId).GetAwaiter().GetResult();

                this.databaseContext.BeginTransaction();
                if (entity == null)
                {
                    var mappingEntity = this.vehicleBusinessMappingData.GetEntity(vehicleBusinessMappingId).GetAwaiter().GetResult();
                    if (mappingEntity == null)
                    {
                        throw new ArgumentException("此记录不存在!");
                    }
                    mappingEntity.AgentRecordId = adminDto.AgentRecord.AgentRecordId;
                    mappingEntity.ProvinceCode = zoneEntity.provinceEntity.ProvinceCode;
                    mappingEntity.CityCode = zoneEntity.cityEntity.CityCode;
                    mappingEntity.CountyCode = zoneEntity.countyEntity.CountyCode;
                    entity = this.vehicleBusinessMappingOverwriteData.Add(mappingEntity).GetAwaiter().GetResult();
                    if (entity == null)
                    {
                        throw new ArgumentException("禁用失败!");
                    }
                }
                var result = this.vehicleBusinessMappingOverwriteData.Disabled(entity.VehicleBusinessMappingOverwriteId.Value, adminDto.AdminId, adminDto.FullName).GetAwaiter().GetResult();
                if (!result)
                {
                    throw new ArgumentException("禁用失败!");
                }
                this.databaseContext.Commit();
                string cacheKey = this.redisService.GetKey(key, vehicleBusinessMappingId, entity.VehicleBusinessMappingOverwriteId);
                this.redisService.DeleteAsync(cacheKey);
                return result;
            }
        }
        #endregion
    }
}